<?php

namespace App\Services;


use App\Models\FsUser;
use App\Models\FsUserHasIm;
use App\Models\SmsRecord;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class SmsService
{
    protected $guzzle;
    protected $smsSetting;

    public function __construct(Client $guzzle)
    {
        $this->guzzle = $guzzle;
        $this->smsSetting = DB::table('sms_settings')->where('status', 1)->first();
    }

    /**
     * @param array $data
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function sendMsm($data)
    {
        $fsUser = FsUser::find($data['id']);
        //获取短信配置
        $sms_value = $this->smsSetting->value;
        $url = $sms_value . "/api/send";
        $postData = [
            'form_params' => [
                'user' => 'auto_'.Auth::user()->name,
                'to_number' => $fsUser->phone_num,
                'text' => $data['text'],
            ],
            'connect_timeout' => 1.5
        ];
        if(!checkMobile($fsUser->phone_num)){
           return ['status' => false, 'msg' =>'错误的手机号码'];
        }
        try{
            $res = $this->guzzle->request('POST',$url,$postData);
            $content = json_decode($res->getBody()->getContents(), true);
            Log::info('单条短信',['res'=>$content, 'data'=>$postData]);
            if(empty($content)){
                return ['status' => false, 'msg' => '短信配置不正确'];
            }
            if ($content['err'] ==1) {
                Log::error('单条短信发送失败',['res'=>$content, 'data'=>$postData]);
                return ['status' => false, 'msg' => $content['err']];
            }
            SmsRecord::create([
                'fs_user_id' => $data['id'],
                'carry_user_id' => Auth::user()->id,
                'value' => $data['text'],
                'status'=> $content['err']==0?1:0
            ]);
            //增加会员短信次数+1
            $this->addSmsNumber($fsUser);
            return ['status' => true];
        }catch (\Exception $e){
            Log::info('单条短信发送失败',['res'=>$e, 'data'=>$postData]);
        }
    }

    public function batchMsm($data){
       foreach ($data['ids'] as $v){
           $fsUser = FsUser::find($v);
           //获取短信配置
           $sms_value = $this->smsSetting->value;
           $url = $sms_value . "/api/send";
           $postData = [
               'form_params' => [
                   'user' => 'auto_'.$data['auth_name'],
                   'to_number' => $fsUser->phone_num,
                   'text' => $data['text'],
               ],
               'connect_timeout' => 1.5
           ];
           $res = $this->guzzle->request('POST',$url,$postData);
           $content = json_decode($res->getBody()->getContents(), true);
           if(empty($content)){
               Log::error('群发短信',['res'=>'短信配置不正确', 'data'=>$postData]);
           }
           if ($content['err']) {
               Log::error('群发短信',['res'=>$content, 'data'=>$postData]);
           }else{
               Log::info('群发短信',['res'=>$content, 'data'=>$postData]);
               SmsRecord::create([
                   'fs_user_id' => $v,
                   'carry_user_id' => $data['auth_id'],
                   'value' => $data['text'],
                   'status'=> $content['err']==0?1:0
               ]);
               //增加会员短信次数+1
               $this->addSmsNumber($fsUser);
           }
       }
    }

    /**
     * 增加会员短信接收次数
     * @param fsUser单个对象
     */
    public function addSmsNumber($fsUser){
        if($fsUser->fsUserHasIm){
           $fsUser->fsUserHasIm->increment('sms_num');
        }else{
            FsUserHasIm::create([
                'fs_user_id' => $fsUser->id,
                'sms_num' => 1
            ]);
        }
    }
}